package org.prototype.bank.service;

import java.util.List;

import org.prototype.bank.dao.AccountDao;
import org.prototype.bank.dao.TransactionDao;
import org.prototype.bank.domain.Account;
import org.prototype.bank.domain.Transaction;
import org.prototype.bank.exceptions.TransferException;
import org.springframework.beans.factory.annotation.Autowired;

public class TransferServiceImpl implements TransferService {

//	private final AccountDao accountDao;
//
//	private final TransactionDao transactionDao;
	
	@Autowired
	private AccountDao accountDao;
	
	@Autowired
	private TransactionDao transactionDao;
	
	public TransferServiceImpl(){
		
	}
	
//	public TransferServiceImpl(AccountDao accountDao,
//			TransactionDao transactionDao) {
//		this.accountDao = accountDao;
//		this.transactionDao = transactionDao;
//	}

	public List<Account> getAccounts(){		
		return accountDao.listAccounts();
	}
	
	public Transaction transfer(String fromAccountId, String toAccountId,
			double amount) throws TransferException {
		Account fromAccount = accountDao.findAccount(fromAccountId);
		Account toAccount = accountDao.findAccount(toAccountId);
		fromAccount.debit(amount);
		toAccount.credit(amount);
		return transactionDao.createTransaction(fromAccount, toAccount, amount);
	}

}
